package jalus.tools;

import jalus.service.ArchiveService;
import jalus.service.DoubanService;
import jalus.service.FileService;
import jalus.service.PicasaService;
import jalus.service.PostService;
import jalus.service.ReaderService;
import jalus.service.TagService;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ActionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException {
		
		String html = "<a href='/action?action=buildfileindex'>创建文件索引表</a><br />"
			+ "<a href='/action?action=transfertag'>tag数据重统计</a><br />"
			+ "<a href='/action?action=clearimagecache'>文件缓存清空</a><br />"
			+ "<a href='/action?action=clearpostcache'>文章缓存清空</a><br />"
			+ "<a href='/action?action=clearexpirecache'>抓取缓存清空</a><br />"
			+ "<a href='/action?action=recountarchive'>archive数据重统计</a><br />"; 
		
		String action = req.getParameter("action");
		if("buildfileindex".equals(action)){
			FileService.getInstance().buildFileIndex();
		}else if("transfertag".equals(action)){
			TagService.getInstance().transfer();
		}else if("clearimagecache".equals(action)){
			FileService.getInstance().clearCache();
		}else if("clearexpirecache".equals(action)){
			ReaderService.getInstance().clearCache();
			DoubanService.getInstance().clearCache();
			PicasaService.getInstance().clearCache();
		}else if("clearpostcache".equals(action)){
			PostService.getInstance().clearCache();
		}else if("recountarchive".equals(action)){
			ArchiveService.getInstance().reCount();
		}
		
        resp.setCharacterEncoding("utf-8"); 
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.write(html);
        out.flush();
	}
	
}
